﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class AdminDAL
    {
        public static int Save(AdminInfo info)
        {
            string sql = string.Format("insert into [admin]([ParentID],[UserName],[UserPwd],[RealName],[Power],[Role],[UserCount],[CreateDate]) values({0},'{1}','{2}','{3}','{4}',{5},{6},'{7}');select @@identity;", info.ParentID, info.UserName, info.UserPwd, info.RealName, info.Power, info.Role, info.UserCount, info.CreateDate);
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql));
        }
        public static int Update(AdminInfo info)
        {
            string sql = string.Format("update [admin] set [UserName]='{0}',[UserPwd]='{1}',[RealName]='{2}',[Power]='{3}',[Role]={4},[UserCount]={5} where [ID]={6}", info.UserName, info.UserPwd, info.RealName, info.Power, info.Role, info.UserCount, info.ID);
            return CommonDAL.ExecuteNoneQuery(sql) ? info.ID : 0;
        }
        public static AdminInfo FindOneByID(int id)
        {
            DataRow row = CommonDAL.FindOneByID(id, "admin");
            return Single_Data(row);
        }


        public static AdminInfo Login(string username, string userpwd)
        {
            DataRow row = CommonDAL.GetDataRow("select * from admin where username='" + username + "' and userpwd='" + userpwd + "'");
            return Single_Data(row);
        }
        public static AdminInfo FindOneById(int id)
        {
            DataRow row = CommonDAL.GetDataRow("select * from admin where id=" + id);
            return Single_Data(row);
        }
        public static List<AdminInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter = "";
            if (B.MyAdmin.Role == 2)
            {
                filter += " and ParentID="+B.MyAdmin.ID;
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                filter += " and ([admin].UserName like '%" + keyword + "%' or [admin].RealName like '%" + keyword + "%')";
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter += " and [admin].CreateDate between '" + DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00") + "' and '" + DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59") + "'";
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[admin]", out count);
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Single_Data(row));
            }
            return list;
        }
        public static List<AdminInfo> FindListByRole(int role)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [admin] where Role={0}", role));
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Single_Data(row));
            }
            return list;
        }
        public static List<AdminInfo> FindListByID(int id)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [admin] where parentid={0} or ID={1}", id, id));
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Single_Data(row));
            }
            return list;
        }
        public static List<AdminInfo> FindAllChildByID(int id)
        {
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ID", id)
            };
            DataTable dt = CommonDAL.GetDataTable("find_allson", paras);
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Single_Data(row));
            }
            return list;
        }
        //获取  没有保存代理商信息 的代理商
        public static List<AdminInfo> FindAllByRole()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select a.* from [admin] a left join [agent] b on a.ID = b.AdminID where Role=2 and b.ID is null"));
            List<AdminInfo> list = new List<AdminInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Single_Data(row));
            }
            return list;
        }
        public static AdminInfo Single_Data(DataRow row)
        {
            AdminInfo info = new AdminInfo();
            if (row != null)
            {
                info.ID = DataConvert.ToInt(row["ID"]);
                info.UserName = DataConvert.ToString(row["UserName"]);
                info.UserPwd = DESEncrypt.Decrypt(DataConvert.ToString(row["UserPwd"]));
                info.RealName = DataConvert.ToString(row["RealName"]);
                info.Power = DataConvert.ToString(row["Power"]);
                info.Role = DataConvert.ToInt(row["Role"]);
                info.UserCount = DataConvert.ToInt(row["UserCount"]);
                info.CreateDate = DataConvert.ToDateTime(row["CreateDate"]);
                info.Parentid = DataConvert.ToInt(row["ParentID"]);

            }
            return info;
        }

    }
}
